package org.example.multi_sys_consist.service;

import org.example.multi_sys_consist.mapper.ApplyMapper;
import org.example.multi_sys_consist.pojo.dto.ApplyApproveVo;
import org.example.multi_sys_consist.pojo.entity.ApplyEntity;
import org.example.multi_sys_consist.pojo.entity.FinanceLog;
import org.example.multi_sys_consist.remote.FinanceLogSystemService;
import org.example.multi_sys_consist.remote.WorkflowService;
import org.example.multi_sys_consist.service.impl.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 申请服务
 */
@Service
public class ApplyService {

    @Autowired
    private CurrentService currentService;
    @Autowired
    private ApplyMapper applyMapper;
    @Autowired
    private WorkflowService bpmService;
    @Autowired
    private LogService logService;
    @Autowired
    private TodoService todoService;
    @Autowired
    private FinanceLogService financeLogService;
    @Autowired
    private FinanceLogSystemService financeLogSystemService;
    @Autowired
    private ApplyService applyService;

    /**
     * 审核通过
     * 1. 记录有任务数据和日志                本地
     * 2. 调用工作流服务-通过                远程写
     * 3. 更新待办人                         远程读+本地
     * 4. 如果审核完成，还需要创建财务日志      本地
     * 5. 推送财务日志                        远程写
     */
    @Transactional(rollbackFor = Exception.class)
    public void approve(Long id, ApplyApproveVo vo) {
        //---------第一步-----------
        //记录业务数据
        applyMapper.saveInfo(vo);
        //记录审核通过日志
        logService.addLog(currentService.currentUser().getAccount() + "审核通过了申请单" + id);
        //删除本地所有待办人
        todoService.deleteByApplyId(id);
        //调用工作流服务
        ApplyEntity applyEntity = applyMapper.selectById(id);
        bpmService.approve(applyEntity.getBpmId());
        //---------第二步-----------
        //更新待办人
        List<Long> todoList = bpmService.getTodoList(applyEntity.getBpmId());
        if (todoList != null && todoList.size() > 0) {
            todoService.save(id, todoList);
        } else {
            //---------第三步-----------
            //没有待办人，则说明已经审完了，生成财务日志，并推送财务日志系统
            List<FinanceLog> financeLog = financeLogService.createFinanceLog(id);
            financeLogSystemService.pushFinanceLog(financeLog);
        }
    }

    public void approve2(Long id, ApplyApproveVo vo) {
        applyService.approveBpm(id, vo);
        List<Long> todoList = applyService.updateTodo(id);
        if (todoList.isEmpty()) {
            List<FinanceLog> financeLog = applyService.createFinanceLog(id);
            applyService.pushFinanceLog(financeLog);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void approveBpm(Long id, ApplyApproveVo vo) {
        //记录业务数据
        applyMapper.saveInfo(vo);
        //记录审核通过日志
        logService.addLog(currentService.currentUser().getAccount() + "审核通过了申请单" + id);
        //删除本地所有待办人
        todoService.deleteByApplyId(id);
        //调用工作流服务
        ApplyEntity applyEntity = applyMapper.selectById(id);
        bpmService.approve(applyEntity.getBpmId());
    }

    @Transactional(rollbackFor = Exception.class)
    public List<Long> updateTodo(Long id) {
        ApplyEntity applyEntity = applyMapper.selectById(id);

        List<Long> todoList = bpmService.getTodoList(applyEntity.getBpmId());
        if (todoList != null && todoList.size() > 0) {
            todoService.save(id, todoList);
        }
        return todoList;
    }

    @Transactional(rollbackFor = Exception.class)
    public List<FinanceLog> createFinanceLog(Long id) {
        return financeLogService.createFinanceLog(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void pushFinanceLog(List<FinanceLog> financeLog) {
        financeLogSystemService.pushFinanceLog(financeLog);
    }
}
